package net.zjitc.dao.impl;

import net.zjitc.dao.CartDao;
import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.utils.db.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/24.
 */
public class CartDaoImpl implements CartDao {
    QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());

    @Override
    public List<CartItem> findByUid(int uid) {
        String sql="SELECT * FROM product p ,Cartitem c WHERE p.pid=c.pid AND c.uid=? ORDER BY orderby";
        List<CartItem> CartitemList=new ArrayList<CartItem>();
        try {
            List<Map<String,Object>> list=runner.query(sql,new MapListHandler(),uid);
            for (Map<String,Object> m: list) {
                Product product=new Product();
                CartItem cartitem=new CartItem();
                BeanUtils.populate(product,m);
                BeanUtils.populate(cartitem,m);
                cartitem.setProduct(product);
                CartitemList.add(cartitem);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(CartitemList.size()>0){
            return CartitemList;
        }
        return null;
    }

    @Override
    public CartItem findByPidAndUid(int pid, int uid) {
        String sql="select * from Cartitem where pid=? and uid=?";

        try {
            Map<String, Object> map=runner.query(sql,new MapHandler(),pid,uid);
            if(map==null){
                return null;
            }
            CartItem Cartitem=new CartItem();
            Product product=new Product();
            User user=new User();
            BeanUtils.populate(Cartitem,map);
            BeanUtils.populate(product,map);
            BeanUtils.populate(user,map);
            Cartitem.setProduct(product);
            Cartitem.setUser(user);
            return Cartitem;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int add(CartItem Cartitem) {
        String sql="insert into Cartitem (cartid, pid,uid ,quantity) values(?,?,?,?)";
        try {
            int count = runner.update(sql,Cartitem.getCartid(),Cartitem.getProduct().getPid(),Cartitem.getUser().getUid(),Cartitem.getQuantity());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(CartItem Cartitem) {
        String sql="update Cartitem set quantity=? where cartid=?";
        try {
            int count=runner.update(sql,Cartitem.getQuantity(),Cartitem.getCartid());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void deleteCart(int pid, int uid) {
        String sql="delete from Cartitem where pid=? and uid=?";
        try {
            runner.update(sql,pid,uid);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteAllCart(int uid) {
        String sql="delete from Cartitem where uid=?";
        try {
            runner.update(sql,uid);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
